package animator.phantom.gui.preview;

/*
    Copyright Janne Liljeblad 2006,2007,2008

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import animator.phantom.gui.GUIResources;
import animator.phantom.gui.modals.render.RenderWindowPanel;
import animator.phantom.controller.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

public class PreViewControlPanel extends JPanel implements ActionListener
{
	//--- Parent window
	private PreViewPanel preViewPanel;

	//---
	public ButtonGroup colorAlpha;

	//--- Display mode
	private ScreenModeButtons screenModeButtons;
	private PreViewRangeSelector rangeSelector;

	//--- Buttons
	private JButton render = new JButton( GUIResources.getIcon( GUIResources.renderPreview));
	private JToggleButton loop = new JToggleButton( GUIResources.getIcon( GUIResources.loop ));
	private JButton stopPreviewRender = new JButton( GUIResources.getIcon( GUIResources.stopPreviewRender ));
	private JButton toPrevious = new JButton(  GUIResources.getIcon(GUIResources.rewind ));
	private JButton toNext = new JButton( GUIResources.getIcon( GUIResources.fastForward ));
	private ImageIcon playIcon =  GUIResources.getIcon( GUIResources.play );
	private ImageIcon pauseIcon =  GUIResources.getIcon( GUIResources.pause );
	private JButton playStop = new JButton( playIcon );
	private JButton toPreviousFrame = new JButton( GUIResources.getIcon(GUIResources.toPreviousFrameNavi ));
	private JButton toNextFrame = new JButton( GUIResources.getIcon( GUIResources.toNextFrameNavi ));
	private JLabel renderInfo = new JLabel();

	//--- Number of frames changed when ff or rewind pressed.
	private int FAST_STEP = 10;
	
	public PreViewControlPanel(PreViewPanel p )
	{
		this.preViewPanel = p;

		screenModeButtons = new ScreenModeButtons( this );
		rangeSelector = new PreViewRangeSelector();

		GUIResources.prepareMediumButton( loop, this, "Loop preview");
		GUIResources.prepareMediumButton( stopPreviewRender, this, "Stop Preview Render");
		GUIResources.prepareMediumButton( toPrevious, this, "Rewind" );
		GUIResources.prepareMediumButton( toPreviousFrame, this, "Previous frame" );
		GUIResources.prepareMediumButton( playStop, this, "Previous frame" );
		GUIResources.prepareMediumButton( toNextFrame, this, "Next frame" );
		GUIResources.prepareMediumButton( toNext, this, "Fast forward" );
		GUIResources.prepareMediumMediumButton( render,this, "Render preview" );

		loop.setSelectedIcon( GUIResources.getIcon( GUIResources.loopPressed ) );
		loop.setSelected( true );
		//loop.setBackground( Color.black );
		//loop.setBorder( null );
		renderInfo.setFont( GUIResources.BASIC_FONT_12 );

		//--- Row1
		setLayout(new BoxLayout( this, BoxLayout.X_AXIS));
		add( Box.createRigidArea( new Dimension( 15, 0 ) ) );

		add( render);
		add( stopPreviewRender );
		add( rangeSelector );
		add( new PreViewSizeSelector() );
		add( new PreViewQualitySelector() );
		add( Box.createRigidArea( new Dimension( 10, 0 ) ) );
		add( toPrevious );
		add( toPreviousFrame );
		add( playStop );
		add( toNextFrame );
		add( toNext );
		add( loop );
		add( Box.createRigidArea( new Dimension( 10, 0 ) ) );
		add( renderInfo );
		add( Box.createHorizontalGlue() );
	}

	public void updatePlayButton()
	{
		if( PreviewController.playbackOn() ) playStop.setIcon( pauseIcon );
		else playStop.setIcon( playIcon );
	}

	public int getSelectedRange(){ return rangeSelector.getSelectedIndex(); }

	public void setInfo( int frame, int renderTime )
	{
		String text = "frame:" + Integer.toString( frame ) + ", time:" + RenderWindowPanel.createTimeString( renderTime, true );
		renderInfo.setText( text );
	}

	public void setInitialText()
	{
		renderInfo.setText( "rendering..." );
	}

	public void clearInfo()
	{
		renderInfo.setText( "" );
	}

	public void actionPerformed( ActionEvent e )
	{
		//--- Test stop and abort before the lock
		if( e.getSource() == playStop ) PreviewController.playPressed();

		if( e.getSource() == render )
		{
			PreviewController.renderAndPlay();
		}
		//--- loop
		if( e.getSource() == loop )
			PreviewController.setLoop( loop.isSelected() );
		//--- abort
		if( e.getSource() == stopPreviewRender )
		{
			PreviewController.abortPreviewRender();
			 clearInfo();
		}
		if( PreviewController.getIsLocked() )return;
		//--- navigate 
		if( e.getSource() == toNextFrame )
		{
			TimeLineController.changeCurrentFrame( 1, this );
			UpdateController.updateCurrentFrameDisplayers( false );
		}
		if( e.getSource() == toPreviousFrame )
		{
			TimeLineController.changeCurrentFrame( -1, this );
			UpdateController.updateCurrentFrameDisplayers( false );
		}
		if( e.getSource() == toNext )
		{
			TimeLineController.changeCurrentFrame( FAST_STEP, this );
			UpdateController.updateCurrentFrameDisplayers( false );
		}
		if( e.getSource() == toPrevious )
		{
			TimeLineController.changeCurrentFrame( -FAST_STEP, this );
			UpdateController.updateCurrentFrameDisplayers( false );
		}
	}

}//end class